package com.itheima.mall.controller;


import com.itheima.mall.pojo.ProductCategory;
import com.itheima.mall.service.PmsProductCategoryService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {

    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;
    //添加商品分类
    @PostMapping("/create")
    public ResponseEntity createProductCategory(@RequestBody ProductCategory pmsProductCategoryParam){
        pmsProductCategoryService.createProductCategory(pmsProductCategoryParam);
        return ResponseEntity.ok("");
    }
    //删除商品分类
    @PostMapping("/delete/{id}")
    public ResponseEntity deleteProductCategoryById(@PathVariable("id") Integer id){
        pmsProductCategoryService.deleteProductCategoryById(id);
        return ResponseEntity.ok("");
    }
    //查询所有一级分类及子分类
    @GetMapping("/list/withChildren")
    public ResponseEntity listWithChildren(){
        List<ProductCategory> categoryList = pmsProductCategoryService.listWithChildren();
        return ResponseEntity.ok(categoryList);

    }
    //分页查询商品分类
    @GetMapping("/list/{parentId}")
    public ResponseEntity selectPageByParentId(@PathVariable("parentId") Integer parentId ,
                                               @RequestParam(defaultValue = "1") Integer pageNum,
                                               @RequestParam(defaultValue = "10") Integer pageSize){
       PageResult pageResult = pmsProductCategoryService.selectPageByParentId(parentId , pageNum , pageSize);
       return ResponseEntity.ok(pageResult);
    }

    //修改导航栏显示状态
    @PostMapping("/update/navStatus")
    public ResponseEntity updateNavStatusByIds(@RequestParam("ids") Integer ids , @RequestParam("navStatus") Integer navStatus){
        pmsProductCategoryService.updateNavStatusByIds(ids , navStatus);
        return ResponseEntity.ok("");
    }

    //修改显示状态
    @PostMapping("/update/showStatus")
    public ResponseEntity updateShowStatus(@RequestParam("ids") Integer ids , @RequestParam("showStatus") Integer showStatus){
        pmsProductCategoryService.updateShowStatus(ids , showStatus);
        return ResponseEntity.ok("");
    }

    //修改商品分类
    @PostMapping("/update/{id}")
    public ResponseEntity updateProductCategoryById(@PathVariable("id") Integer id , @RequestBody ProductCategory productCategoryParam){
        pmsProductCategoryService.updateProductCategoryById(id , productCategoryParam);
        return ResponseEntity.ok("");
    }
    @GetMapping("/{id}")
    public ResponseEntity selectProductCategoryById(@PathVariable("id") Integer id){


       ProductCategory productCategory = pmsProductCategoryService.selectProductCategoryById(id);

       return ResponseEntity.ok(productCategory);

    }


}
